package com.zmt.mrms.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zmt.mrms.entity.HospitalSection;
import com.zmt.mrms.entity.MedicalRecord;
import com.zmt.mrms.entity.PaymentRecord;
import com.zmt.mrms.entity.PrescriptionRecord;
import com.zmt.mrms.entity.UserAdmin;
import com.zmt.mrms.entity.UserDoctor;
import com.zmt.mrms.entity.UserPatient;

@Mapper
public interface AdminMapper {

	/**
	 * 病历管理模块
	 **/

	@Select("select * from payment_record where medical_record_id=#{id}")
	PaymentRecord findPayRecordByMedRecId(@Param("id") int id);

	@Select("select * from prescription_record where medical_record_id=#{id}")
	PrescriptionRecord findPreRecordByMedRecId(@Param("id") int id);

	@Select("select * from medical_record where patient_idcard=#{idcard}")
	List<MedicalRecord> findMedRecordsByIdcard(@Param("idcard") String idcard);

	@Select("select * from medical_record")
	List<MedicalRecord> findAllMedicalRecords();

	/**
	 * 患者管理模块
	 **/

	@Update("update user_patient set status=#{status} where id=#{id}")
	int changePatientStatus(@Param("id") int id, @Param("status") boolean status);

	@Select("select * from user_patient where name like #{fuzzyQuery} or sex like #{fuzzyQuery} or email like #{fuzzyQuery} or phone like #{fuzzyQuery} or identify_card like #{fuzzyQuery}")
	List<UserPatient> findSomePatient(@Param("fuzzyQuery") String fuzzyQuery);

	@Select("select * from user_patient")
	List<UserPatient> findAllPatient();

	/**
	 * 医师管理模块
	 **/

	@Update("update user_doctor set section=#{section},position=#{position},name=#{name},sex=#{sex},birthday=#{birthday},idcard=#{idcard},role=#{role} where id=#{id}")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	int updateDoctor(UserDoctor userDoctor);

	@Insert("insert into user_doctor(section,position,name,sex,birthday,idcard,status,role,account,password) values(#{section},#{position},#{name},#{sex},#{birthday},#{idcard},true,#{role},#{account},#{password})")
	@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
	int addDoctor(UserDoctor userDoctor);

	@Select("select * from user_doctor where id=#{id}")
	UserDoctor findOneDcotor(@Param("id") int id);

	@Update("update user_doctor set status=#{status} where id=#{id}")
	int changeDoctorStatus(@Param("id") int id, @Param("status") boolean status);

	@Select("select * from user_doctor where name like #{fuzzyQuery} or sex like #{fuzzyQuery} or email like #{fuzzyQuery} or idcard like #{fuzzyQuery}")
	List<UserDoctor> findFuzzyDoctor(@Param("fuzzyQuery") String fuzzyQuery);

	@Select("select * from hospital_section")
	List<HospitalSection> findAllSection();

	@Select("select * from user_doctor")
	List<UserDoctor> findAllDoctor();

	/**
	 * 管理员个人信息管理模块
	 **/

	@Update("update user_admin set account=#{account},password=#{pwd},status=false,idcard=null where id=#{id}")
	int resetAccount(@Param("account") String newAccount, @Param("pwd") String newPwd, @Param("id") int id);

	@Update("update user_admin set account=#{account},password=#{pwd} where id=#{id}")
	int updateAccountInfo(@Param("account") String account, @Param("pwd") String pwd, @Param("id") int id);

	@Select("select * from user_admin where id=#{id}")
	UserAdmin findAdminbyId(@Param("id") int id);

	/**
	 * 管理员登录模块
	 **/

	@Update("update user_admin set idcard=#{idcard},status=true where id=#{id}")
	int updateIdcard(@Param("idcard") String idcard, @Param("id") int id);

	@Select("select * from user_admin where account=#{account} and password=#{password}")
	UserAdmin findAdmin(UserAdmin userAdmin);

}
